package com.wwwarehouse.contract.convert_to_pc;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.bean.response.GetImportToolIndexBean;
import com.wwwarehouse.common.common.Common;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.custom_widget.time_pick.ChoosePickerDialog;
import com.wwwarehouse.common.eventbus_event.PushImportEvent;
import com.wwwarehouse.common.eventbus_event.StartImportEvent;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.contract.bean.PushImportBean;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * Created by feng.gao on 2017/9/4.
 * 手机转PC 批量导入 需要手动选择分类的页面 这个页面产品说先不做 ，就放着欣赏吧 ，简直完美。
 */
@Route(path = "/contract/BulkImportFragment")
public class BulkImportFragment extends BaseFragment implements View.OnClickListener {
	private TextView mSelectSortTv; //选择分类标题
	private TextView mWebAddressTv; //生成的短连接地址
	private ImageView mShareQQiv; //QQ分享
	private ImageView mShareWechatIv; //微信分享
	private LinearLayout mUnselectSortLayout; //未选择分类之前的布局
	private LinearLayout mSelectSortLayout; //选择分类之后的布局
	public static final int GET_TYPE_REQUEST_CODE = 0;
	public static final int GET_SHORT_URL_CODE = 1;
	private List<String> mTypeBean;
	private Map<String, Object> map = new HashMap<>();
	private GetImportToolIndexBean bean;
	@Override
	public void setTitle() {
		if (peekFragment() instanceof BulkImportFragment) {
			(mActivity).setTitle(R.string.unselect_sort_title);
		}
	}

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		return inflater.inflate(R.layout.bulk_import_fragment_layout , container ,false);
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		EventBus.getDefault().register(this);
		mSelectSortTv = (TextView) view.findViewById(R.id.selection_sort_tv);
		mWebAddressTv = (TextView) view.findViewById(R.id.web_address_tv);
		mShareQQiv = (ImageView) view.findViewById(R.id.share_qq_iv);
		mShareWechatIv = (ImageView) view.findViewById(R.id.share_wechat_iv);
		mUnselectSortLayout = (LinearLayout) view.findViewById(R.id.unselect_sort_layout);
		mUnselectSortLayout.setVisibility(View.VISIBLE);
		mSelectSortLayout = (LinearLayout) view.findViewById(R.id.select_sort_layout);
		mSelectSortLayout.setVisibility(View.GONE);
		mShareQQiv.setOnClickListener(this);
		mShareWechatIv.setOnClickListener(this);
		mSelectSortTv.setOnClickListener(this);
		if (getArguments() != null && getArguments().getSerializable(ContractConstant.KEY_INTENT_IMPORT_TOOL_BEAN) != null) {
			GetImportToolIndexBean bean = (GetImportToolIndexBean) getArguments().getSerializable(ContractConstant.KEY_INTENT_IMPORT_TOOL_BEAN);
			if (bean != null && bean.getTemplateTypeList() != null) {
				mTypeBean = bean.getTemplateTypeList();
			}
		}
	}

	@Override
	public void requestDatas() {
	}

	@Override
	public void onClick(View v) {
		int i = v.getId();
		if (i == R.id.selection_sort_tv) {
			if (mTypeBean != null) {
				new ChoosePickerDialog.Builder(mActivity).setData(mTypeBean).setTitle("导入的分类").setOnDataSelectedListener(new ChoosePickerDialog.OnSelectedListener() {
					@Override
					public void onSelected(String s, int i) {
						mSelectSortTv.setText(s);
						Map<String, Object> map = new HashMap<>();
						map.put("businessUnitId", 10002018);
						//有没有分类你自己心里没有一点逼数吗 templateType
						map.put("templateType", s);
						map.put("operationUkid", null);
						NoHttpUtils.httpPost(ContractConstant.GET_IMPORT_TOOLS_INDEX, map, new NoHttpUtils.OnResponseListener() {
							@Override
							public void onSucceed(CommonClass commonClass, int i) {
								if (i == 0) {
									if (commonClass != null && commonClass.getData() != null) {
										bean = JSON.parseObject(commonClass.getData().toString(), GetImportToolIndexBean.class);
										if (bean != null && bean.getUrl() != null) {
											mUnselectSortLayout.setVisibility(View.GONE);
											mSelectSortLayout.setVisibility(View.VISIBLE);
											mWebAddressTv.setText("操作网址：" + bean.getUrl());
										}
									}
								}

							}

							@Override
							public void onFailed(String s, int i) {

							}

							@Override
							public void onFinish(int i) {

							}
						}, 0);

					}

					@Override
					public void onCancel() {

					}
				}).create().show();
			}
		} else if (i == R.id.share_qq_iv) {
			if (bean != null && bean.getUrl() != null) {
				Common.getInstance().shareQQ(mActivity , bean.getUrl(),"" , "" ,bean.getUrl() );
			}
		} else if (i == R.id.share_wechat_iv) {
			if (bean != null && bean.getUrl() != null) {
				Common.getInstance().shareWeixinTextMoment(mActivity, bean.getUrl());
			}

		}
	}


	/**
	 * 接收推送消息，跳转到导入成功页面。
	 * @param pushImportEvent
	 */
	public void onEventMainThread(PushImportEvent pushImportEvent){
		if (peekFragment() instanceof BulkImportFragment) {
			PushImportBean pushImportBean = JSON.parseObject(pushImportEvent.getResult(), PushImportBean.class);
			if (pushImportBean != null && pushImportBean.getMsg() != null) {
				Bundle bundle = new Bundle();
				bundle.putLong(Constant.KEY_INTENT_IMPORT_TOOL_PUSH_CLICK, Long.valueOf(pushImportBean.getMsg().getPushImportCount()));
				ImportSuccessFragment importSuccessFragment = new ImportSuccessFragment();
				importSuccessFragment.setArguments(bundle);
				pushFragment(importSuccessFragment);
			}
		}
	}

	/**
	 * 接收推送消息，跳转到计时页面。
	 * @param startImportEvent
	 */
	public void onEventMainThread(StartImportEvent startImportEvent){
		if (peekFragment() instanceof BulkImportFragment) {
			PushImportBean mStartImportBean = JSON.parseObject(startImportEvent.getResult(), PushImportBean.class);
			if (mStartImportBean != null && mStartImportBean.getMsg() != null) {
				Bundle bundle = new Bundle();
				bundle.putLong(Constant.KEY_INTENT_IMPORT_TIME, Long.valueOf(mStartImportBean.getMsg().getCostTime()));
				DurationTimeFragment durationTimeFragment = new DurationTimeFragment();
				durationTimeFragment.setArguments(bundle);
				pushFragment(durationTimeFragment);
			}
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}

}
